package com.gzx.plugin.fin.tech.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.gzx.plugin.fin.tech.dto.GzxFinTechBacktrackReqDTo;
import com.gzx.plugin.fin.tech.dto.GzxTestExcelDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import vip.xiaonuo.common.util.CommonExcelUtil;
import vip.xiaonuo.common.util.CommonHttpUtil;
import vip.xiaonuo.common.util.CommonRandomUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class StressTestScoringEngine {

    static String excelFilePath = "/Users/zjt/Desktop/测试样本0630-1.xlsx";
    static String resultFilePath = "/Users/zjt/Desktop/测试样本0630-1_实时.xlsx";

    public static void main(String[] args) throws Exception {
        testLocalExcel();
    }

    private static void testLocalExcel() {
        try {
            List<GzxTestExcelDto> testExcelDtoList = CommonExcelUtil.importExcel(excelFilePath, GzxTestExcelDto.class);
            List<List<GzxTestExcelDto>> batchList = Lists.partition(testExcelDtoList, 2000);
            Map<String, Object> headers = buildHeaders();
            List<GzxTestExcelDto> resultList = new ArrayList<>();
            for (List<GzxTestExcelDto> batch : batchList) {
                List<String> phoneList = batch.stream().map(GzxTestExcelDto::getPhone).toList();
                String phoneRespBody = CommonHttpUtil.post("http://117.72.49.18:9105/api/fin-tech/score/forward", headers, JSON.toJSONString(Map.of("phone", phoneList)), "");
                String dataStr = JSON.parseObject(phoneRespBody).getString("data");
                String phoneStr = JSON.parseObject(dataStr).getString("phoneScores");
                Map<String, Integer> phoneMap = JSON.parseObject(phoneStr, Map.class);
                List<String> cardIdList = new ArrayList<>();
                for (GzxTestExcelDto dto : batch) {
                    if (phoneMap.containsKey(dto.getPhone())) {
                        if (phoneMap.get(dto.getPhone()) > 0) {
                            dto.setScore(phoneMap.get(dto.getPhone()));
                            resultList.add(dto);
                        } else if (StringUtils.isNotBlank(dto.getCardId())) {
                            cardIdList.add(dto.getCardId());
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(cardIdList)) {
                    String cardIdRespBody = CommonHttpUtil.post("http://117.72.49.18:9105/api/fin-tech/score/forward", headers, JSON.toJSONString(Map.of("cardId", cardIdList)), "");
                    String dataStr1 = JSON.parseObject(cardIdRespBody).getString("data");
                    String cardIdStr = JSON.parseObject(dataStr1).getString("cardIdScores");
                    Map<String, Integer> cardIdMap = JSON.parseObject(cardIdStr, Map.class);
                    for (GzxTestExcelDto dto : batch) {
                        if (cardIdMap.containsKey(dto.getCardId())) {
                            if (cardIdMap.get(dto.getCardId()) > 0) {
                                dto.setScore(cardIdMap.get(dto.getCardId()));
                                resultList.add(dto);
                            }
                        }
                    }
                }
            }
            CommonExcelUtil.exportExcel(resultFilePath, resultList, GzxTestExcelDto.class);
        } catch (Exception e) {
            log.error("Local Excel test failed", e);
        }
    }


    private static void testBacktrackLocalExcel() {
        try {
            List<GzxTestExcelDto> testExcelDtoList = CommonExcelUtil.importExcel(excelFilePath, GzxTestExcelDto.class);
            List<List<GzxTestExcelDto>> batchList = Lists.partition(testExcelDtoList, 2000);
            Map<String, Object> headers = buildHeaders();
            List<GzxTestExcelDto> resultList = new ArrayList<>();
            for (List<GzxTestExcelDto> batch : batchList) {
                List<GzxFinTechBacktrackReqDTo> body = new ArrayList<>();
                for (GzxTestExcelDto testExcelDto : batch) {
                    GzxFinTechBacktrackReqDTo finTechBacktrackReqDTo = new GzxFinTechBacktrackReqDTo();
                    finTechBacktrackReqDTo.setId(testExcelDto.getPhone());
                    finTechBacktrackReqDTo.setDate(testExcelDto.getDate());
                    body.add(finTechBacktrackReqDTo);
                }
                String phoneRespBody = CommonHttpUtil.post("http://117.72.49.18:9105/api/fin-tech/score/backtrack", headers, JSON.toJSONString(Map.of("phone", body)), "");
                String dataStr = JSON.parseObject(phoneRespBody).getString("data");
                String phoneStr = JSON.parseObject(dataStr).getString("phoneScores");
                Map<String, Integer> phoneMap = JSON.parseObject(phoneStr, Map.class);
                body = new ArrayList<>();
                for (GzxTestExcelDto dto : batch) {
                    if (phoneMap.containsKey(dto.getPhone())) {
                        if (phoneMap.get(dto.getPhone()) > 0) {
                            dto.setScore(phoneMap.get(dto.getPhone()));
                            resultList.add(dto);
                        } else if (StringUtils.isNotBlank(dto.getCardId())) {
                            GzxFinTechBacktrackReqDTo finTechBacktrackReqDTo = new GzxFinTechBacktrackReqDTo();
                            finTechBacktrackReqDTo.setId(dto.getCardId());
                            finTechBacktrackReqDTo.setDate(dto.getDate());
                            body.add(finTechBacktrackReqDTo);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(body)) {
                    String cardIdRespBody = CommonHttpUtil.post("http://117.72.49.18:9105/api/fin-tech/score/backtrack", headers, JSON.toJSONString(Map.of("cardId", body)), "");
                    String dataStr1 = JSON.parseObject(cardIdRespBody).getString("data");
                    String cardIdStr = JSON.parseObject(dataStr1).getString("cardIdScores");
                    Map<String, Integer> cardIdMap = JSON.parseObject(cardIdStr, Map.class);
                    for (GzxTestExcelDto dto : batch) {
                        if (cardIdMap.containsKey(dto.getCardId())) {
                            if (cardIdMap.get(dto.getCardId()) > 0) {
                                dto.setScore(cardIdMap.get(dto.getCardId()));
                                resultList.add(dto);
                            }
                        }
                    }
                }
            }
            CommonExcelUtil.exportExcel(resultFilePath, resultList, GzxTestExcelDto.class);
        } catch (Exception e) {
            log.error("Local Excel test failed", e);
        }
    }

    private static Map<String, Object> buildHeaders() {
        Map<String, Object> headers = new HashMap<>();
        String apiKey = "gzx";//请求Key
        String apiSecret = "zjtnb";//请求密钥
        long timestamp = System.currentTimeMillis();//当前时间戳
        headers.put("Content-Type", "application/json");
        headers.put("apiKey", apiKey);
        headers.put("apiSecret", apiSecret);
        headers.put("timestamp", String.valueOf(timestamp));
        //生成随机字符串
        String nonce = CommonRandomUtil.generateRandomString(16, true, true, true, false);
        headers.put("nonce", nonce);
        String signature = DigestUtils.md5Hex(nonce + apiKey + apiSecret + timestamp);//加密
        headers.put("signature", signature);
        return headers;
    }
}
